﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Retirement_topUpPremium : System.Web.UI.Page
{

    Dictionary<string, string> targetPercentage = new Dictionary<string, string>();


    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btn_premiumTopUpRequest_Click(object sender, ImageClickEventArgs e)
    {
        int custId = -1;

        if (Session["userID"] == null)
        {
            custId = 15369472;//TODO GET FROM SESSION

        }
        else
        {
            custId = Convert.ToInt32(Session["userID"]);

        }

        List<Policy> policies = Policy.getPolicy(custId);
        Policy policy = policies.ElementAt(0);

        Requests requests = new Requests();
        PolicyRules policyRules = new PolicyRules();

        if (cb_metRepresentative.Checked)
        {
            requests.metRepresentative = "Y";
        }
        else
        {
            requests.metRepresentative = "N";
        }

        requests.policyNo = policy.policyNo;
        requests.totalAmount = Convert.ToDecimal(this.tb_topUpAmount.Text);

        try
        {
            this.getTopUpFundRequest();

            if (!this.cb_agree.Checked)
            {
                throw new RetirementException(RetirementException.CHECK_BOX_UNCHECKED);
            }

            requests.fundTopUpRequest(targetPercentage);
            this.Response.Redirect("./requestSuccessful.aspx");

        }
        catch (RetirementException ex)
        {
            this.lb_exceptionMsg.Text = ex.message;
        }
       
    }

    protected void btn_cancelRequest_Click(object sender, ImageClickEventArgs e)
    {
        this.Response.Redirect("./myPortfolio.aspx");
    }

    protected void btn_fundSwitchRequest_Click(object sender, ImageClickEventArgs e)
    {
        Requests requests = new Requests();
        PolicyRules policyRules = new PolicyRules();

        if (cb_metRepresentative.Checked)
        {
            requests.metRepresentative = "Y";
        }
        else
        {
            requests.metRepresentative = "N";
        }

        //if (rb_savingsAccount.Checked == true)
        //{
        //    requests.accountId = policyRules.getAccountId(this.tb_nric.Text, "Savings");
        //}
        //else
        //{
        //    requests.accountId = policyRules.getAccountId(this.tb_nric.Text, "Current");
        //}

        requests.policyNo = "FP2013";
        requests.totalAmount = Convert.ToDecimal(this.tb_topUpAmount.Text);

        this.getTopUpFundRequest();
        requests.fundTopUpRequest(targetPercentage);
        this.Response.Redirect("./requestSuccessful.aspx");
    }


    private void getTopUpFundRequest()
    {
        //targetPercentage
        if (!Util.IsBlank(ddl_topUpFund1.SelectedValue))
        {
            targetPercentage.Add(ddl_topUpFund1.SelectedValue, this.tb_topUpFundPercentage1.Text);
        }

        if (!Util.IsBlank(ddl_topUpFund2.SelectedValue))
        {
            targetPercentage.Add(ddl_topUpFund2.SelectedValue, this.tb_topUpFundPercentage2.Text);
        }

        if (!Util.IsBlank(ddl_topUpFund3.SelectedValue))
        {
            targetPercentage.Add(ddl_topUpFund3.SelectedValue, this.tb_topUpFundPercentage3.Text);
        }

        if (!Util.IsBlank(ddl_topUpFund4.SelectedValue))
        {
            targetPercentage.Add(ddl_topUpFund4.SelectedValue, this.tb_topUpFundPercentage4.Text);
        }

        if (!Util.IsBlank(ddl_topUpFund5.SelectedValue))
        {
            targetPercentage.Add(ddl_topUpFund5.SelectedValue, this.tb_topUpFundPercentage5.Text);
        }

        PolicyRules policyRules = new PolicyRules();

        if (targetPercentage.Keys.Count == 0)
        {
            return;
        }

        decimal totalPercentage = 0;
        foreach (string percentage in targetPercentage.Values)
        {
            totalPercentage += Convert.ToDecimal(percentage);
        }

        if (totalPercentage != 100)
        {
            return;
        }

        //  policyRules.switchFund(policyNo, sourcePercentageOrAmount, targetPercentage);

    }



}